import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shop/http/HttpUtil.dart';
import 'package:shop/route/my_route.dart';
import 'package:shop/shop/life/Login.dart';
class GaiMima extends StatefulWidget {
  GaiMima({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _GaiMima createState() => _GaiMima();
}

class _GaiMima extends State<GaiMima> {

  // 构造方法
  _GaiMima()  {
    isLogin();
  }
  // 判断是否登录
  Future<void> isLogin() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    if(prefs.getBool("isLogin")==false){
      Fluttertoast.showToast(
          msg: "请先登录",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.BOTTOM,
          timeInSecForIosWeb: 1,
          backgroundColor: Colors.black45,
          textColor: Colors.white,
          fontSize: 16.0
      );
      MyRoute.replace(Login());
    }
  }

  TextEditingController _oldpwdController = new TextEditingController();
  TextEditingController _pwdController = new TextEditingController();
  TextEditingController _pwdController2 = new TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text("修改密码"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              child: TextFormField(
                // maxLines: 1,
                // maxLength: 12,
                obscureText: true,
                controller:_oldpwdController ,
                decoration: InputDecoration(
                  hintText: "请输入原密码",
                  labelText: "请输入原密码",
                ),
                validator: (val){
                  if(val.trim().length<1)
                    return "密码不能为空";
                  return null;
                },
                onSaved: (str) {
                  _oldpwdController.text = str;//str为文本的内容值
                },
              ),
            ),Container(
              child: TextFormField(
                // maxLines: 1,
                // maxLength: 12,
                obscureText: true,
                controller: _pwdController,
                decoration: InputDecoration(
                  hintText: "密码至少6位",
                  labelText: "请输入新密码",
                ),
                validator: (val){
                  if(val.trim().length<1)
                    return "密码不能为空";
                  return null;
                },
                onSaved: (str) {
                  _pwdController.text = str;//str为文本的内容值
                },
              ),
            ),
            Container(
              child: TextFormField(
                // maxLines: 1,
                // maxLength: 12,
                obscureText: true,
                controller: _pwdController2,
                decoration: InputDecoration(
                  hintText: "密码至少6位",
                  labelText: "请再次输入新密码",
                ),
                validator: (val){
                  if(val.trim().length<1)
                    return "密码不能为空";
                  return null;
                },
                onSaved: (str) {
                  _pwdController2.text = str;//str为文本的内容值
                },
              ),
            ),
            Container(
              child: RaisedButton(
                shape: RoundedRectangleBorder(
                    ),
                onPressed: () async {
                  if(_oldpwdController.text.trim().length<6)
                  {
                    Fluttertoast.showToast(
                        msg: "密码不能为空且不小于6位",
                        toastLength: Toast.LENGTH_SHORT,
                        gravity: ToastGravity.BOTTOM,
                        timeInSecForIosWeb: 1,
                        backgroundColor: Colors.black45,
                        textColor: Colors.white,
                        fontSize: 16.0
                    );
                    return ;
                  }
                  if(_pwdController.text.trim().length<6)
                  {
                    Fluttertoast.showToast(
                        msg: "密码不能为空且不小于6位",
                        toastLength: Toast.LENGTH_SHORT,
                        gravity: ToastGravity.BOTTOM,
                        timeInSecForIosWeb: 1,
                        backgroundColor: Colors.black45,
                        textColor: Colors.white,
                        fontSize: 16.0
                    );
                    return ;
                  }
                  if(_pwdController.text.trim()!=_pwdController2.text.trim())
                  {
                    Fluttertoast.showToast(
                        msg: "两次密码不一致",
                        toastLength: Toast.LENGTH_SHORT,
                        gravity: ToastGravity.BOTTOM,
                        timeInSecForIosWeb: 1,
                        backgroundColor: Colors.black45,
                        textColor: Colors.white,
                        fontSize: 16.0
                    );
                    return ;
                  }
                  // Response response = await Dio().post("http://xllm.szghrj.com:8082/app/user/regist",data:{"userName":_pwdController.text.trim(),"userPhone":_pwdController.text.trim(),"userPassword":_pwdController.text.trim()});
                  // var x = json.decode(response.toString());
                  SharedPreferences prefs = await SharedPreferences.getInstance();
                  String userId = prefs.getString("userId");
                  var x = await HttpUtil().get("/app/user/resPassword?oldPassword=${_oldpwdController.text.trim()}&newPassword=${_pwdController.text.trim()}&type=0&userId=${userId}");
                  if(x['code']==200){
                    Fluttertoast.showToast(
                        msg: "密码修改成功",
                        toastLength: Toast.LENGTH_SHORT,
                        gravity: ToastGravity.BOTTOM,
                        timeInSecForIosWeb: 1,
                        backgroundColor: Colors.black45,
                        textColor: Colors.white,
                        fontSize: 16.0
                    );
                    Navigator.pushReplacement(
                        context,
                        MaterialPageRoute(
                            builder:(context)=>Login()
                        )
                    );
                  }
                },
                child: Text("修改",
                   ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}